from typing import Dict, Any, Optional

from pydantic import BaseModel, Field


class PageDTO(BaseModel):
    """
    通用分页查询DTO，可复用
    """
    page: int = Field(1, ge=1, description="当前页码，从 1 开始")
    page_size: int = Field(10, ge=1, le=9999, description="每页条数")
    sort_by: str = Field("id", description="排序字段")
    sort_order: str = Field("ASC", pattern="^(ASC|DESC)$", description="排序顺序")
    filters: Optional[Dict[str, Any]] = Field(None, description="过滤条件，字段->值 或 字段->值列表 映射")


# TypeError: cannot convert dictionary update sequence element #0 to a sequence
def list_row_dict(list_of_rows):
    result_list = []

    for row in list_of_rows:
        values = []
        field_names = []

        for value in row:
            values.append(value)

        field_indexes = row._key_to_index
        for field_name in field_indexes:
            field_names.append(field_name)

        row_dict = dict(zip(field_names, values))
        result_list.append(row_dict)

    return result_list
